
<div style="margin-left: 94%;">
    <a class="layui-btn layui-btn-primary layui-btn-sm operate" operate-type="add"><i class="layui-icon">&#xe608;</i>添加</a>
</div>

<table class="layui-table">
    <thead>
    <tr>
        <th width="5%;">ID</th>
        <th width="10%;">权限名称</th>
        <th width="10%;">描述</th>
        <th width="10%;">权限值</th>
        <th width="10%;">操作</th>
    </tr>
    </thead>
    <tbody>

    </tbody>
</table>
<!--分页start-->
<div id="paginate"></div>
<!--分页end-->

<script>
    $(function(){
        GetTableList();
    });

    //获取表格数据
    function GetTableList(page=1,pagesize=10){
        var url = '/handler/permission/getPowerList';
        var data = {page:page,pagesize:pagesize};
        dink.post(url,data,function(res){
            if(res.ret == 1){
                var target = $('tbody');
                var html = '';
                $.each(res.data,function(i,ele){
                    html += '<tr>\n' +
                        '    <td>'+(i+1)+'</td>\n' +
                        '    <td>'+ele.name+'</td>\n' +
                        '    <td>'+ele.description+'</td>\n' +
                        '    <td>'+ele.value+'</td>\n' +
                        '    <td>\n' +
                        '        <a class="layui-btn layui-btn-xs operate" operate-type="edit" data-id="'+ele.id+'">编辑</a>\n' +
                        '        <a class="layui-btn layui-btn-xs operate" operate-type="delete" data-id="'+ele.id+'">删除</a>\n' +
                        '    </td>\n' +
                        '</tr>';
                });
                target.html(html);
                if(page == 1){
                    Paginate(res.total,pagesize);
                }
            }else{
                var target = $('tbody');
                var html = '<tr><td colspan="5">无数据</td></tr>';
                target.html(html);
                layer.msg(res.msg);return;
            }
        });
    }

    //分页
    function Paginate(count,pagesize){
        layui.use('laypage',function(){
            var laypage = layui.laypage;
            laypage.render({
                elem: 'paginate',
                count: count,
                limit: pagesize,
                layout: ['count', 'prev', 'page', 'next', 'skip'],
                jump: function(obj,first){
                    if(!first){
                        GetTableList(obj.curr,pagesize);
                    }
                }
            });
        });
    }

    //操作按钮click事件
    dink.click('.operate',function(){
        var operate_type = $(this).attr('operate-type');
        var id = $(this).attr('data-id');
        //判断权限
        dink.post('/handler/permission/checkOperatePermission',{permission:'permission:power:'+operate_type},function(res){
            if(res.ret == 1){
                AdminPermissionPowerOpreate(operate_type,id);
            }else{
                layer.msg(res.msg);return;
            }
        });
        //end
    });

    //添加/编辑权限值
    function AdminPermissionPowerOpreate(operate='add',id=0){
        if(operate == 'add'){
            dink.html('AdminPermissionPowerAdd','添加权限值',['400px','400px']);
        }else if(operate == 'edit'){
            dink.html('AdminPermissionPowerAdd','编辑权限值',['400px','400px']);
            dink.post('/handler/permission/getPowerDetailById',{id:id},function(res){
                var item = res.data;
                $('input[name=name]').val(item.name);
                $('input[name=description]').val(item.description);
                $('input[name=value]').val(item.value.split(':').pop());
                setTimeout(function(){
                    $('select[name=powers] option[data-id="'+item.pid+'"]').attr('selected',true);
                },200)
            });
        }else if(operate == 'delete'){
            dink.alert('Tips','确定删除？',function(){
                dink.post('/handler/permission/deletePower',{id:id},function(res){
                    layer.msg(res.msg);
                    GetTableList();return;
                });
            });
        }else{
            layer.msg('操作类型异常');return;
        }
        //添加、编辑
        dink.click('#add',function(){
            var data = {};
            var base = {};
            if(operate == 'add'){
                var url = '/handler/permission/addPower';
            }else if(operate == 'edit'){
                var url = '/handler/permission/editPower';
                if(id == 0){
                    layer.msg('缺少记录id');return;
                }
                data.id = id;
            }else{
                layer.msg('操作类型异常');return;
            }
            base.name = $('input[name=name]').val();
            base.description = $('input[name=description]').val();
            base.pid = $('select[name=powers] option:selected').attr('data-id');
            if($('select[name=powers]').val()==0){
                base.value = $('input[name=value]').val();
            }else{
                base.value = $('select[name=powers]').val()+':'+$('input[name=value]').val();
            }
            data.base = base;
            dink.alert('Tips','确定'+(operate=='add' ? '添加' : '编辑')+'权限值？',function(){
                dink.post(url,data,function(res){
                    if(res.ret == 1){
                        dink.close(res.msg);
                        GetTableList();
                    }else{
                        layer.msg(res.msg);
                    }
                });
            });
        });

    }
</script>